﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ServiceGerenciador.Model;

public partial class paginas_Agenda : System.Web.UI.Page
{

    public IEnumerable<Agendamento> ApontamentosHoje
    {
        get
        {
            return Agendamento.FindAll().Where(s => s.Date.Date.Equals(DateTime.Today));
        }
    }
    


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CreateTimeSpan();
            //Cria uma data / object tempo que define um período de tempo

            ddFuncionario.DataSource = Funcionario.FindAll();
            ddFuncionario.DataTextField = "Nome";
            ddFuncionario.DataValueField = "Id";

            ddPessoa.DataSource = Person.FindAll();
            ddPessoa.DataTextField = "Nome";
            ddPessoa.DataValueField = "Id";

            ddServico.DataSource = Service.FindAll();
            ddServico.DataTextField = "servico";
            ddServico.DataValueField = "idServico";

            ddFuncionario.DataBind();
            ddPessoa.DataBind();
            ddServico.DataBind();
        }
    }

    private void CreateTimeSpan()
    {
        List<TimeSpan> horas = new List<TimeSpan>();

        horas = new List<TimeSpan>();

        for (int i = 6; i <= 18; i++)
        {

            horas.Add(new TimeSpan(i, 0, 0));
            horas.Add(new TimeSpan(i, 30, 0));

        }

        rptSchedule.DataSource = horas;
        rptSchedule.ItemDataBound += rptSchedule_ItemDataBound;
        rptSchedule.DataBind();
        
        ddHorasInicial.DataSource = horas;
        ddHorasInicial.DataBind();

        ddHorasFinal.DataSource = horas;
        ddHorasFinal.DataBind();
    }

    private void rptSchedule_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            TimeSpan ts = (TimeSpan)e.Item.DataItem;

            HtmlGenericControl spanActions = e.Item.FindControl("spanActions") as HtmlGenericControl;
            spanActions.Visible = false;

            foreach (var apontamento in ApontamentosHoje)
            {
                TimeSpan tsInitial = TimeSpan.Parse(apontamento.InitialDateTime);
                TimeSpan tsFinal = TimeSpan.Parse(apontamento.FinalDateTime);

                if (ts >= tsInitial && ts < tsFinal)
                {
                    Literal litHalfHour1 = (e.Item.FindControl("litHalfHour1") as Literal);
                    litHalfHour1.Text = apontamento.Description;
                    spanActions.Visible = true;


                    ImageButton imgEdit = e.Item.FindControl("imgEdit") as ImageButton;
                    ImageButton imgDelete = e.Item.FindControl("imgDelete") as ImageButton;

                    imgEdit.CommandArgument = Convert.ToString(apontamento.Id);
                    imgDelete.CommandArgument = Convert.ToString(apontamento.Id);
                }
            }
        }
    }

    protected void btnAdicionar_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {

            Agendamento agendamento;
            if (this.AgendamentoEdit == null)
                agendamento = new Agendamento();
            else
                agendamento = this.AgendamentoEdit;

            agendamento.Servico = Service.Find(Convert.ToInt32(ddServico.SelectedValue));
            agendamento.Funcionario = Funcionario.Find(Convert.ToInt32(ddFuncionario.SelectedValue));

            agendamento.Date = DateTime.Today;

            agendamento.InitialDateTime = ddHorasInicial.SelectedValue;
            agendamento.FinalDateTime = ddHorasFinal.SelectedValue;
            agendamento.Description = txtDescricao.Text;
            agendamento.Obs = txtObs.Text;

            agendamento.Save();
            ClearPage();
            CreateTimeSpan();
        }
    }

    public Agendamento AgendamentoEdit
    {
        get
        {
            return ViewState["AgendamentoEdit"] as Agendamento;
        }
        set
        {
            ViewState["AgendamentoEdit"] = value;
        }
    }

    protected void rptSchedule_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case ("Editar"):
                Agendamento agendamento = Agendamento.Find(Convert.ToInt32(e.CommandArgument));

                this.AgendamentoEdit = agendamento;

                this.txtDescricao.Text = this.AgendamentoEdit.Description;
                this.txtObs.Text = this.AgendamentoEdit.Obs;


                this.ddFuncionario.SelectedIndex = -1;
                this.ddServico.SelectedIndex = -1;
                this.ddHorasInicial.SelectedIndex = -1;
                this.ddHorasFinal.SelectedIndex = -1;


                this.ddFuncionario.Items.FindByValue(Convert.ToString(this.AgendamentoEdit.Funcionario.Id)).Selected = true;
                this.ddServico.Items.FindByValue(Convert.ToString(this.AgendamentoEdit.Servico.idServico)).Selected = true;

                this.ddHorasInicial.Items.FindByValue(this.AgendamentoEdit.InitialDateTime).Selected = true;
                this.ddHorasFinal.Items.FindByValue(this.AgendamentoEdit.FinalDateTime).Selected = true;

                break;
            case ("Apagar"):
                Agendamento.DeleteAll(String.Format("Id = {0}", e.CommandArgument));
                CreateTimeSpan();
                break;
        }
    }

    protected void ClearPage() 
    {
        this.ddFuncionario.SelectedIndex = -1;
        this.ddServico.SelectedIndex = -1;
        this.ddHorasInicial.SelectedIndex = -1;
        this.ddHorasFinal.SelectedIndex = -1;


        this.txtDescricao.Text = string.Empty;
        this.txtObs.Text = string.Empty;

        this.AgendamentoEdit = null;

    }

}
